package com.ying.tests.CCCbasis.Test4Thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test4ThreadDemo {
    public static void main(String[] args) {
//        test1();        // 继承Thread类
//        test2();        // 实现Runnable接口
//        test3();        // 使用Callable和Future
        test4();        // demo
    }

    private static void test1() {
        Test4Thread r1 = new Test4Thread("Thread-1");
        r1.start();
        Test4Thread r2 = new Test4Thread("Thread-2");
        r2.start();
    }

    private static void test2() {
        Test4Runnable r1 = new Test4Runnable("Thread-1");
        r1.start();
        Test4Runnable r2 = new Test4Runnable("Thread-2");
        r2.start();
    }

    private static void test3() {
        Test4Callable callable = new Test4Callable();
        FutureTask futureTask = new FutureTask<>(callable);
        for (int i = 0; i < 100; i ++) {
            System.out.println(Thread.currentThread().getName() + "的循环变量i的值" + i);
            if (20 == i) {
                new Thread(futureTask, "有返回值的线程").start();
            }
        }
        try {
            System.out.println("子线程的返回值" + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static void test4() {
        Runner1 runner1 = new Runner1();
        Runner2 runner2 = new Runner2();
        Thread thread1 = new Thread(runner1);
        Thread thread2 = new Thread(runner2);

//        thread1.start();// 多线程执行
//        thread2.start();// 多线程执行

        thread1.run();// 是顺序执行
        thread2.run();// 是顺序执行
    }
}

class Runner1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i ++) {
            System.out.println("进入Runner1运行状态——————————" + i);
        }
    }
}
class Runner2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i ++) {
            System.out.println("进入Runner2运行状态——————————" + i);
        }
    }
}
